/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.service
 * 创建时间：2020年03月31日 12时51分
 **/
package com.zhongwang.mms.module.purchase.service;


import com.zhongwang.mms.base.BaseService;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.PAllocationRecord;
import com.zhongwang.mms.gen.entity.PInventory;
import com.zhongwang.mms.module.purchase.constant.PurchaseConstant;
import com.zhongwang.mms.module.purchase.dao.*;
import com.zhongwang.mms.gen.entity.POrder;
import com.zhongwang.mms.gen.entity.POrderDetail;
import com.zhongwang.mms.module.purchase.model.*;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.BigDecimalUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.zhongwang.mms.config.timingtask.StatisticalTimingTask.getVWProjectByCode;

/**
 * @ClassName： AllocationManageService
 * @Description： 调拨业务逻辑层
 * @Author： 徐一贺
 * @Date： 2020/3/31 12:51
 * @Version： V1.0
 **/
@Log4j2
@Service
public class PurAllocationManageService extends BaseService {

    @Autowired
    private PurSystemDao purSystemDao;
    @Autowired
    private PurAllocationManageDao purAllocationManageDao;
    @Autowired
    private PurPOrderDao purPOrderDao;
    @Autowired
    private PurPOrderDetailDao purPOrderDetailDao;
    @Autowired
    private PurPInventoryDao purPInventoryDao;
    @Autowired
    private PurAllocationRecordDao purAllocationRecordDao;
    @Autowired
    private PurTDeptDao purTDeptDao;
    @Autowired
    private LogService logService;
    @Autowired
    private PurWarOutManageService purWarOutManageService;
    @Autowired
    private PurChangeDao purchangeDao;
    @Autowired
    private PurTProjectDao purTProjectDao;

    /**
     * @FunctionName: getCurrentDate
     * @Description: 获取数据库服务器当前日期
     * @Author: 徐一贺
     * @CreateDate: 2020/3/26 15:37
     * @Phone: 18241927380
     * @Return: String 数据库服务器当前日期
     * @Version: 1.0.0
     */
    public String getCurrentDate() {
        return purSystemDao.getCurrentDate();
    }

    /**
     * @FunctionName: findLastPRKOrderCode
     * @Description: 获取最新出库单编码
     * @设计思路： 1.判断年后两位+月+日与当前年月日是否相同，如果相同且流水号则在流水号上递增1。
     * 2.如果数据为空则按照以PRK+年（后两位）月日+001生成外购件出库单编码。
     * 3.如果不相同则按照以PRK+年（后两位）月日+001生成外购件出库单编码。
     * @Author: 徐一贺
     * @CreateDate: 2020/3/17 14:22
     * @Phone: 18241927380
     * @Param:
     * @Return: String 最新出库单编码
     * @Version:
     */
    private String generatePDBOrderCode() {
        String newGeneratePRKOrderCode = "";
        //获取最后编码
        String lastPCKOrderCode = findLastPDBOrderCode();
        //获取当前时间简码
        String datetimeCode = new SimpleDateFormat("yyMMdd").format(new Date());
        if (lastPCKOrderCode == null) {
            newGeneratePRKOrderCode = "PDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else if (!lastPCKOrderCode.substring(3, 9).equals(datetimeCode)) {
            newGeneratePRKOrderCode = "PDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + "001";
        } else {
            //获取外购件库最新最后的入库单号
            Integer sn = Integer.parseInt(lastPCKOrderCode.substring(9));
            newGeneratePRKOrderCode = "PDB" + new SimpleDateFormat("yyMMdd").format(new Date()) + String.format("%03d", (sn + 1)); //25为int型 ;
        }
        return newGeneratePRKOrderCode;
    }

    /**
     * @FunctionName: findLastPDBOrderCode
     * @Description: 获取最后出库的编码
     * @Author: 徐一贺
     * @CreateDate: 2020/3/17 14:17
     * @Phone: 18241927380
     * @Return: String
     * @Version: 1.0.0
     */
    public String findLastPDBOrderCode() {
        return purAllocationManageDao.findLastPDBOrderCode();
    }


    /**
     * @FunctionName: createAllocationOrder
     * @Description: 创建调拨库单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/3 11:29
     * @Phone: 18241927380
     * @Param: param 创建使用的参数
     * @Return: String 返回出库单编号
     * @Version: 1.0.0
     */
    public String createAllocationOrder(PInventoryQueryModel param) {
        String fromCode = generatePDBOrderCode();
        //获取库存信息
        List<PInventoryModel> pInventoryModelList = purPInventoryDao.findInventoryListByIds(param.getItyIds());
        for (PInventoryModel pInventoryModel : pInventoryModelList) {
            PAllocationRecord pAllocationRecord = new PAllocationRecord();
            pAllocationRecord.setArdItyId(pInventoryModel.getItyId());
            pAllocationRecord.setArdMatCode(pInventoryModel.getItySapCode());
            pAllocationRecord.setArdSapMatDesc(pInventoryModel.getOdaMatDesc());
            pAllocationRecord.setArdProName(pInventoryModel.getOdaProName());
            pAllocationRecord.setArdProCode(pInventoryModel.getOdaProCode());
            pAllocationRecord.setArdNum(pInventoryModel.getOdaNum());
            pAllocationRecord.setArdUnit(pInventoryModel.getOdaNumUnit());
            pAllocationRecord.setArdWhiId(pInventoryModel.getItyWhiId());
            pAllocationRecord.setArdPosition(pInventoryModel.getOdaPosition());
            pAllocationRecord.setArdTakeType(pInventoryModel.getOdaTakeType());
            pAllocationRecord.setArdInWhiId(param.getInWarehouseId());
            pAllocationRecord.setArdTakeType("制作中");
            pAllocationRecord.setArdType(1);
            pAllocationRecord.setArdExecStatus(0);
            pAllocationRecord.setArdChangeFlg(0);
            pAllocationRecord.setArdRequester(param.getOdrTakePerson());
            pAllocationRecord.setArdDepId(param.getDepId());
            pAllocationRecord.setArdDataStatus(1);
            pAllocationRecord.setArdUserAccount(ShiroUtils.getUser().getAccount());
            pAllocationRecord.setArdGenDate(new Date());
            pAllocationRecord.setArdFormCode(fromCode);
            pAllocationRecord.setArdTrackCode(pInventoryModel.getOdaTrackCode());
            pAllocationRecord.setArdSpec(param.getOdrRemark());
            pAllocationRecord.setArdBrand(pInventoryModel.getItyBrand());
            pAllocationRecord.setArdItyRemark(pInventoryModel.getItyRemark());
            pAllocationRecord.setArdPrice(pInventoryModel.getItyPrice());
            pAllocationRecord.setArdAmount(pInventoryModel.getItyAmount());
            pAllocationRecord.setArdStockType(pInventoryModel.getItyStockType());
            pAllocationRecord.setArdBookkeepingDate(pInventoryModel.getItyBookkeepingDate());
            purAllocationRecordDao.insert(pAllocationRecord);
            pAllocationRecord.setArdRelationId(pAllocationRecord.getArdId());
            pAllocationRecord.setArdType(2);
            purAllocationRecordDao.insert(pAllocationRecord);
        }
        return fromCode;
    }

    /**
     * @FunctionName: createAllocationOrderHaveFormCode
     * @Description: 创建有单号的调拨数据
     * @DesignIdea: 创建有单号的调拨数据
     * @Author: 徐一贺
     * @CreateDate: 2020/5/23 12:51
     * @Phone: 18241927380
     * @Param: [pInventoryModelList] 库存列表，[param]主单信息
     * @Return: void
     * @Throw: 无异常捕获
     * @Version: v 1.0.0
     */
    public void createAllocationOrderHaveFormCode(List<PInventoryModel> pInventoryModelList, PInventoryQueryModel param) {
        for (PInventoryModel pInventoryModel : pInventoryModelList) {
            PAllocationRecord pAllocationRecord = new PAllocationRecord();
            pAllocationRecord.setArdItyId(pInventoryModel.getItyId());
            pAllocationRecord.setArdMatCode(pInventoryModel.getItySapCode());
            pAllocationRecord.setArdSapMatDesc(pInventoryModel.getItyMatDesc());
            pAllocationRecord.setArdProName(pInventoryModel.getItyProName());
            pAllocationRecord.setArdProCode(pInventoryModel.getItyProCode());
            pAllocationRecord.setArdNum(pInventoryModel.getItyNum());
            pAllocationRecord.setArdUnit(pInventoryModel.getItyNumUnit());
            pAllocationRecord.setArdWhiId(pInventoryModel.getItyWhiId());
            pAllocationRecord.setArdPosition(pInventoryModel.getItyPosition());
            pAllocationRecord.setArdInWhiId(param.getInWarehouseId());
            pAllocationRecord.setArdTakeType("制作中");
            pAllocationRecord.setArdType(1);
            pAllocationRecord.setArdExecStatus(0);
            pAllocationRecord.setArdChangeFlg(0);
            pAllocationRecord.setArdRequester(param.getOdrTakePerson());
            pAllocationRecord.setArdDepId(param.getDepId());
            pAllocationRecord.setArdDataStatus(1);
            pAllocationRecord.setArdUserAccount(ShiroUtils.getUser().getAccount());
            pAllocationRecord.setArdGenDate(new Date());
            pAllocationRecord.setArdFormCode(param.getOdrCode());
            pAllocationRecord.setArdTrackCode(pInventoryModel.getItyTrackCode());
            pAllocationRecord.setArdSpec(param.getOdrRemark());
            pAllocationRecord.setArdBrand(pInventoryModel.getItyBrand());
            pAllocationRecord.setArdItyRemark(pInventoryModel.getItyRemark());
            pAllocationRecord.setArdPrice(pInventoryModel.getItyPrice());
            pAllocationRecord.setArdAmount(pInventoryModel.getItyAmount());
            pAllocationRecord.setArdStockType(pInventoryModel.getItyStockType());
            pAllocationRecord.setArdRemark(param.getOdrRemark());
            pAllocationRecord.setArdBookkeepingDate(pInventoryModel.getItyBookkeepingDate());
            purAllocationRecordDao.insert(pAllocationRecord);
            pAllocationRecord.setArdType(2);
            pAllocationRecord.setArdRelationId(pAllocationRecord.getArdId());
            purAllocationRecordDao.insert(pAllocationRecord);
        }
    }

    /**
     * @FunctionName: findAllocationOrderListByPDBCode
     * @Description: 通过调拨单码获取状态为非删除的数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/3 16:54
     * @Phone: 18241927380
     * @Param: odrCode 调拨单码
     * @Return: List<PInventoryModel> 调拨单码获取状态为非删除的数据
     * @Version: 1.0.0
     */
    public List<PAllocationRecordModel> findAllocationOrderListByPDBCode(String odrCode) {
        return purAllocationManageDao.findAllocationOrderListByPDBCode(odrCode);
    }

    /**
     * @FunctionName: findPAllocationRecordByPrimaryKey
     * @Description: 根据主键查找调拨单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 9:31
     * @Phone: 18241927380
     * @Param: ardId 主键
     * @Return: PAllocationRecord 查找的调拨单
     * @Version: 1.0.0
     */
    public PAllocationRecord findPAllocationRecordByPrimaryKey(Integer ardId) {
        return purAllocationRecordDao.selectByPrimaryKey(ardId);
    }

    /**
     * @FunctionName: delPAllocationRecordByPrimaryKey
     * @Description: 根据主键删除调拨单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 9:32
     * @Phone: 18241927380
     * @Param: ardId 主键
     * @Return: void 空
     * @Version: 1.0.0
     */
    public void delPAllocationRecordByPrimaryKey(PAllocationRecord pAllocationRecord) {
        try {
            pAllocationRecord.setArdDataStatus(0);
            purAllocationRecordDao.updateByPrimaryKeySelective(pAllocationRecord);
            purAllocationRecordDao.delToUpdateByArdPid(pAllocationRecord.getArdId());
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }

    /**
     * @FunctionName: 验证
     * @Description: 请填写方法描述
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 13:10
     * @Phone: 18241927380
     * @Param: param 被验证参数
     * @Return: boolean 验证结果
     * @Version: 1.0.0
     */
    public boolean checkNum(PAllocationRecordModel param) {
        Double avaNumber = purPInventoryDao.findByPrimaryKey(param.getArdItyId()).getItyAvaNum();
        param.setArdDataStatus(1);
        List<PAllocationRecordModel> pAllocationRecordList = purAllocationRecordDao.findChildList(param);
        for (PAllocationRecordModel pAllocationRecord : pAllocationRecordList) {
            avaNumber = avaNumber - pAllocationRecord.getArdNum();
        }
        return avaNumber - param.getArdNum() > 0;
    }

    /**
     * @FunctionName: 验证
     * @Description: 请填写方法描述
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 13:10
     * @Phone: 18241927380
     * @Param: param 被验证参数
     * @Return: boolean 验证结果
     * @Version: 1.0.0
     */
    public boolean checkNum2(PAllocationRecordModel param) {
        Double avaNumber = purPInventoryDao.findByPrimaryKey(param.getArdItyId()).getItyAvaNum();
        param.setArdDataStatus(1);
        List<PAllocationRecordModel> pAllocationRecordList = purAllocationRecordDao.findChildList(param);
        for (PAllocationRecordModel pAllocationRecord : pAllocationRecordList) {
            if (pAllocationRecord.getArdType() == 2) {
                avaNumber = avaNumber - pAllocationRecord.getArdNum();
            }
        }
        return avaNumber >= 0;
    }

    /**
     * @FunctionName: createPAllocationRecord
     * @Description: 创建新库存
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 13:26
     * @Phone: 18241927380
     * @Param: param 传递参数
     * @Return: void 空
     * @Version: 2.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public PAllocationRecordModel createPAllocationRecord(PAllocationRecordModel param) {
        try {
            PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdPid());
            pAllocationRecord.setArdProCode(param.getArdProCode());
            pAllocationRecord.setArdProName(param.getArdProName());
            pAllocationRecord.setArdNum(new BigDecimal(param.getArdNum()).doubleValue());
            pAllocationRecord.setArdType(2);
            pAllocationRecord.setArdExecStatus(0);
            pAllocationRecord.setArdChangeFlg(0);
            pAllocationRecord.setArdDataStatus(1);
            pAllocationRecord.setArdUserAccount(ShiroUtils.getUser().getAccount());
            pAllocationRecord.setArdGenDate(new Date());
            pAllocationRecord.setArdPid(param.getArdPid());
            pAllocationRecord.setArdAmount(BigDecimalUtils.mul(pAllocationRecord.getArdNum(), pAllocationRecord.getArdPrice()));
//            pAllocationRecord.setArdDepId(purTDeptDao.findByDeptName(param.getDepName()).getDepId());
            purAllocationRecordDao.insertSelective(pAllocationRecord);
            PAllocationRecord changeSourceArdNumModel = new PAllocationRecord();
            changeSourceArdNumModel.setArdId(pAllocationRecord.getArdPid());
//            List<PAllocationRecordModel> childList = purAllocationRecordDao.findChildList(param);
//            Double childTotalArdNum = 0d;
//            for (PAllocationRecordModel childModel:childList) {
//                childTotalArdNum= BigDecimalUtils.add(childTotalArdNum,childModel.getArdNum());
//            }
            Double result = BigDecimalUtils.sub(purAllocationRecordDao.selectByPrimaryKey(param.getArdPid()).getArdNum(), pAllocationRecord.getArdNum());
            changeSourceArdNumModel.setArdNum(result.doubleValue());
            changeSourceArdNumModel.setArdAmount(BigDecimalUtils.mul(changeSourceArdNumModel.getArdNum(), pAllocationRecord.getArdPrice()));
            purAllocationRecordDao.updateByPrimaryKeySelective(changeSourceArdNumModel);
            return purAllocationRecordDao.findPAllocationRecordModelByPrimaryKey(pAllocationRecord.getArdId());
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error(ex.getMessage());
        }
        return null;
    }

    /**
     * @FunctionName: update
     * @Description: 修改
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 17:57
     * @Phone: 18241927380
     * @Param: param 被修改的参数
     * @Return: PAllocationRecordModel 返回修改完毕后的数据
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public PAllocationRecordModel update(PAllocationRecordModel param) {
        PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdId());
        Double avaNumber = purAllocationRecordDao.selectByPrimaryKey(param.getArdPid()).getArdNum();
        Double total = BigDecimalUtils.add(avaNumber, pAllocationRecord.getArdNum());
        Double result = BigDecimalUtils.sub(total, param.getArdNum());
        if (result > 0) {
            param.setArdAmount(BigDecimalUtils.mul(pAllocationRecord.getArdPrice(), param.getArdNum()));
            purAllocationRecordDao.updateByPAllocationRecordModelPrimaryKeySelective(param);
            PAllocationRecord changeSourceArdNumModel = new PAllocationRecord();
            changeSourceArdNumModel.setArdId(pAllocationRecord.getArdPid());
            changeSourceArdNumModel.setArdNum(result);
            changeSourceArdNumModel.setArdAmount(BigDecimalUtils.mul(pAllocationRecord.getArdPrice(), changeSourceArdNumModel.getArdNum()));
            purAllocationRecordDao.updateByPrimaryKeySelective(changeSourceArdNumModel);
            return purAllocationRecordDao.findPAllocationRecordModelByPrimaryKey(param.getArdId());
        }
        return null;
    }

    /**
     * @FunctionName: delPAllocationRecord
     * @Description: 根据调拨单号删除领料单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 18:49
     * @Phone: 18241927380
     * @Param: ardId 主键
     * @Version: 1.0.0
     */
    public boolean delPAllocationRecord(String odrCode) {
        try {
            purAllocationRecordDao.deleteByArdFormCode(odrCode);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @FunctionName: delPAllocationRecord
     * @Description: 根据主键删除领料单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 18:49
     * @Phone: 18241927380
     * @Param: ardId 主键
     * @Version: 1.0.0
     */
    public boolean delPAllocationRecord(PAllocationRecord pAllocationRecord) {
        try {
            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @FunctionName: checkListNum
     * @Description: 检测数量数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 20:35
     * @Phone: 18241927380
     * @Param: param 参数
     * @Return: String 返回消息
     * @Version: 1.0.0
     */
    public String checkListNum(PAllocationRecordCreateModel param) {
        List<PAllocationRecordModel> pAllocationRecordModelList = purAllocationManageDao.findAllocationOrderListByPDBCode(param.getArdFormCode());
        for (int i = 0; i < pAllocationRecordModelList.size(); i++) {
            if (pAllocationRecordModelList.get(i).getArdType() == 2) {
                if (checkNum2(pAllocationRecordModelList.get(i)) == false) {
                    return "物料描述为： " + pAllocationRecordModelList.get(i).getArdSapMatDesc() + " 的可用库存不足，请修改后再保存！";
                }
            }
        }
        return null;
    }

    /**
     * @FunctionName: create
     * @Description: 保存调拨单
     * @Author: 徐一贺
     * @CreateDate: 2020/4/6 19:20
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: 2.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public POrderPrintModel create(PAllocationRecordCreateModel param) {
        POrderPrintModel pOrderPrintModel = new POrderPrintModel();
        Integer odrId;
        Integer odaId;
        try {
            //根据调拨单号更新领料人和领料车间
            purAllocationRecordDao.updatePurAllocationBytArdFormCode(param);
            List<PAllocationRecordModel> pAllocationRecordModelList = purAllocationManageDao.findAllocationOrderListByPDBCode(param.getArdFormCode());
            if (pAllocationRecordModelList.size() > 0) {
                //创建主单数据
                POrder pOrder = new POrder();
                pOrder.setOdrCode(purWarOutManageService.getOrderCode(PurchaseConstant.PCK));
                pOrder.setOdrTakePerson(param.getOdrTakePerson());
                pOrder.setOdrDate(param.getOdrDate());
                pOrder.setOdrMakePersonId(ShiroUtils.getUser().getId());
                pOrder.setOdrChangeFlag(0);
                pOrder.setOdrType(2);
                pOrder.setOdrRedFlag(0);
                pOrder.setOdrReqFlag(1);
                pOrder.setOdrDate(param.getOdrDate());
                pOrder.setOdrMakeDate(new Date());
                if (param.getOdrTakeWorkshopId() != 0) {
                    pOrder.setOdrTakeWorkshopId(param.getOdrTakeWorkshopId());
                }
                pOrder.setOdrRemark(param.getOdrRemark());
                purPOrderDao.insert(pOrder);
                odrId = pOrder.getOdrId();
                logService.saveLog("创建调拨出库单", "创建的调拨出库单号为：" + pOrder.getOdrCode());
                //创建明细数据
                //1.获取库存信息
                for (int i = 0; i < pAllocationRecordModelList.size(); i++) {
                    if (pAllocationRecordModelList.get(i).getArdType() == 1) {
                        POrderDetail pOrderDetail = new POrderDetail();
                        pOrderDetail.setOdaOdrId(odrId);
                        pOrderDetail.setOdaType(2);
                        pOrderDetail.setOdaRedFlag(0);
                        PInventory pInventory = purPInventoryDao.findByPrimaryKey(pAllocationRecordModelList.get(i).getArdItyId());
                        pOrderDetail.setOdaProCode(pInventory.getItyProCode());
                        pOrderDetail.setOdaProName(pInventory.getItyProName());
                        pOrderDetail.setOdaMatCode(pInventory.getItyMatCode());
                        pOrderDetail.setOdaSapCode(pInventory.getItySapCode());
                        pOrderDetail.setOdaMatDesc(pInventory.getItyMatDesc());
                        pOrderDetail.setOdaNum(pAllocationRecordModelList.get(i).getArdNum());
                        pOrderDetail.setOdaNumUnit(pInventory.getItyNumUnit());
                        pOrderDetail.setOdaPosition(pInventory.getItyPosition());
                        pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                        pOrderDetail.setOdaOutWhiId(param.getOdaOutWhiId());
                        pOrderDetail.setOdaInWhiId(param.getInWarehouseId());
                        pOrderDetail.setOdaTrackCode(pInventory.getItyTrackCode());
                        pOrderDetail.setOdaSpec(pInventory.getItyRemark());
                        pOrderDetail.setOdaTakeType(pAllocationRecordModelList.get(i).getArdTakeType());
                        pOrderDetail.setOdaOutItyId(pInventory.getItyId());
                        pOrderDetail.setOdaWhiId(pInventory.getItyWhiId());
                        pOrderDetail.setOdaCaseNo(pInventory.getItyCaseNo());
                        pOrderDetail.setContractNo(pInventory.getContractNo());
                        pOrderDetail.setOdaBrand(pInventory.getItyBrand());
                        //插入数据
                        purPOrderDetailDao.insert(pOrderDetail);
                        odaId = pOrderDetail.getOdaId();
                        pAllocationRecordModelList.get(i).setArdOdrId(odrId);
                        pAllocationRecordModelList.get(i).setArdOdaId(odaId);
                        pAllocationRecordModelList.get(i).setArdInWhiId(param.getInWarehouseId());
                        pAllocationRecordModelList.get(i).setArdExecOutPersonId(ShiroUtils.getUserId());
                        pAllocationRecordModelList.get(i).setArdExecOutDate(new Date());
                        //更新调拨数据外键
                        purAllocationRecordDao.updateByModel(pAllocationRecordModelList.get(i));
                        //更改库存
                        pInventory.setItyAvaNum(0d);
                        pInventory.setItyNum(0d);
                        purPInventoryDao.updateByPrimaryKeySelective(pInventory);
                    } else {
                        pAllocationRecordModelList.get(i).setArdInWhiId(param.getInWarehouseId());
                        pAllocationRecordModelList.get(i).setArdExecOutPersonId(ShiroUtils.getUserId());
                        pAllocationRecordModelList.get(i).setArdExecOutDate(new Date());
                        //更新调拨数据外键
                        purAllocationRecordDao.updateByModel(pAllocationRecordModelList.get(i));
                    }
                }
                //更改调拨单为出库状态

                param.setArdExecOutPersonId(ShiroUtils.getUser().getId());
                param.setArdExecOutDate(new Date());
                purAllocationRecordDao.execAllocation(param);
                logService.saveLog("创建调拨单", "创建的调拨单号为：" + param.getArdFormCode());
                POrderDetail pOrderDetailObj = new POrderDetail();
                pOrderDetailObj.setOdaOdrId(odrId);
                pOrderPrintModel.setPOrder(purPOrderDao.selectPOrderById(odrId));
                pOrderPrintModel.setPOrderDetailList(purPOrderDetailDao.selectByWhere(pOrderDetailObj));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
        return pOrderPrintModel;
    }

    /**
     * @FunctionName: updatePAllocationRecord
     * @Description: 根据单据号更新领料人和领料车间
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 7:50
     * @Phone: 18241927380
     * @Param: param 单据号、领料人和领料车间
     * @Return: List<PAllocationRecordModel>  根据单据号返回非删除列表
     * @Version: 1.0.0
     */
    public void updatePAllocationRecord(PAllocationRecordModel param) {
        purAllocationRecordDao.updateRequesterAndTakeWorkshopByFormCode(param);
    }

    /**
     * @FunctionName: findNoCreateOrderList
     * @Description: 根据当前登录账号查询该账号下未创建的调拨单列表
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 8:59
     * @Phone: 18241927380
     * @Param: account 当前用户账号
     * @Return: List<PAllocationRecordModel> 当前登录账号查询该账号下未创建的调拨单列表
     * @Version: 1.0.0
     */
    public List<PAllocationRecordModel> findNoCreateOrderList(String account) {
        return purAllocationRecordDao.findNoCreateOrderList(account);
    }

    /**
     * @FunctionName: addAllocationOutWarehouseRecordData
     * @Description: 根据主键增加领料出库数据，并且返回该调拨单的所有领料出库数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 12:59
     * @Phone: 18241927380
     * @Param: param 参数
     * @Return: List<PAllocationRecordModel> 该调拨单的所有领料出库数据
     * @Version: 2.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addAllocationOutWarehouseRecordData(PAllocationRecordModel param) {
        JsonResult jsonResult = new JsonResult();
        PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdId());
        PAllocationRecord existPAllocationRecordQueryParam = new PAllocationRecord();
        existPAllocationRecordQueryParam.setArdType(3);
        existPAllocationRecordQueryParam.setArdRelationId(param.getArdId());
        PAllocationRecord existPAllocationRecord = purAllocationRecordDao.getPAllocationRecordBySelective(existPAllocationRecordQueryParam);
        if (null != existPAllocationRecord) {
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("领料出库已存在此条出库记录，不可重复添加！");
        } else {
            pAllocationRecord.setArdType(3);
            pAllocationRecord.setArdRelationId(param.getArdId());
            pAllocationRecord.setArdRequester(param.getArdRequester());
            pAllocationRecord.setArdDepId(param.getArdDepId());
            purAllocationRecordDao.insertSelective(pAllocationRecord);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("添加领料出库成功！");
            jsonResult.setData(pAllocationRecord.getArdFormCode());
        }
        return jsonResult;
    }

//    /**
//     * @FunctionName: delAllocationOutWarehouseRecordData
//     * @Description: 根据主键删除领料出库数据，并且返回该调拨单的所有领料出库数据
//     * @Author: 徐一贺
//     * @CreateDate: 2020/4/7 12:59
//     * @Phone: 18241927380
//     * @Param: param 参数
//     * @Return: List<PAllocationRecordModel> 该调拨单的所有领料出库数据
//     * @Version: 1.0.0
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public String delAllocationOutWarehouseRecordData(PAllocationRecordModel param) {
//        PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdId());
//        if (pAllocationRecord.getArdType().equals(3)) {
//            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
//        } else if (pAllocationRecord.getArdType().equals(2)) {
//            PAllocationRecord parentModel = purAllocationRecordDao.selectByPrimaryKey(pAllocationRecord.getArdPid());
//            PAllocationRecord changeArdNumModel = new PAllocationRecord();
//            changeArdNumModel.setArdId(parentModel.getArdId());
//            changeArdNumModel.setArdNum(BigDecimalUtils.add(parentModel.getArdNum(),pAllocationRecord.getArdNum()));
//            changeArdNumModel.setArdAmount(BigDecimalUtils.mul(changeArdNumModel.getArdNum(), parentModel.getArdPrice()));
//            purAllocationRecordDao.updateByPrimaryKeySelective(changeArdNumModel);
//            PAllocationRecord deleteTakeModelParam = new PAllocationRecord();
//            deleteTakeModelParam.setArdRelationId(pAllocationRecord.getArdId());
//            purAllocationRecordDao.deleteByArdRelationId(deleteTakeModelParam);
//            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
//        } else if (pAllocationRecord.getArdType().equals(1)) {
//            pAllocationRecord.setArdType(2);
//            List<PAllocationRecord> inModels = purAllocationRecordDao.selectByArdRelationIdAndArdType(pAllocationRecord);
//            for (PAllocationRecord inModel : inModels) {
//                PAllocationRecord getTakeModelParam = new PAllocationRecord();
//                getTakeModelParam.setArdId(pAllocationRecord.getArdId());
//                getTakeModelParam.setArdType(3);
//                List<PAllocationRecord> takeModels = purAllocationRecordDao.selectByArdRelationIdAndArdType(getTakeModelParam);
//                for (PAllocationRecord takeModel : takeModels) {
//                    purAllocationRecordDao.deleteByPrimaryKeySelective(takeModel);
//                }
//                purAllocationRecordDao.deleteByPrimaryKeySelective(inModel);
//            }
//            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
//        }
//        return pAllocationRecord.getArdFormCode();
//    }

    /**
     * @FunctionName: delAllocationOutWarehouseRecordData
     * @Description: 根据主键删除领料出库数据，并且返回该调拨单的所有领料出库数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 12:59
     * @Phone: 18241927380
     * @Param: param 参数
     * @Return: List<PAllocationRecordModel> 该调拨单的所有领料出库数据
     * @Version: 2.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public String delAllocationOutWarehouseRecordData(PAllocationRecordModel param) {
        PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdId());
        if (pAllocationRecord.getArdType().equals(2)) {
            PAllocationRecord parentModel = purAllocationRecordDao.selectByPrimaryKey(pAllocationRecord.getArdPid());
            PAllocationRecord changeArdNumModel = new PAllocationRecord();
            changeArdNumModel.setArdId(parentModel.getArdId());
            changeArdNumModel.setArdNum(BigDecimalUtils.add(parentModel.getArdNum(), pAllocationRecord.getArdNum()));
            changeArdNumModel.setArdAmount(BigDecimalUtils.mul(changeArdNumModel.getArdNum(), parentModel.getArdPrice()));
            purAllocationRecordDao.updateByPrimaryKeySelective(changeArdNumModel);
//            PAllocationRecord deleteTakeModelParam = new PAllocationRecord();
//            deleteTakeModelParam.setArdRelationId(pAllocationRecord.getArdId());
//            purAllocationRecordDao.deleteByArdRelationId(deleteTakeModelParam);
            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
        } else if (pAllocationRecord.getArdType().equals(1)) {
            pAllocationRecord.setArdType(2);
            List<PAllocationRecord> inModels = purAllocationRecordDao.selectByArdRelationIdAndArdType(pAllocationRecord);
            for (PAllocationRecord inModel : inModels) {
//                PAllocationRecord getTakeModelParam = new PAllocationRecord();
//                getTakeModelParam.setArdId(pAllocationRecord.getArdId());
//                getTakeModelParam.setArdType(3);
//                List<PAllocationRecord> takeModels = purAllocationRecordDao.selectByArdRelationIdAndArdType(getTakeModelParam);
//                for (PAllocationRecord takeModel : takeModels) {
//                    purAllocationRecordDao.deleteByPrimaryKeySelective(takeModel);
//                }
                purAllocationRecordDao.deleteByPrimaryKeySelective(inModel);
            }
            purAllocationRecordDao.deleteByPrimaryKeySelective(pAllocationRecord);
        }
        return pAllocationRecord.getArdFormCode();
    }

    /**
     * @FunctionName: loadAllocationRecord
     * @Description: 根据主键加载调拨入库 调拨出库 领料单数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 13:36
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version:
     */
    public List<PAllocationRecordModel> loadAllocationRecord(PAllocationRecordModel param) {
        PAllocationRecord pAllocationRecord = purAllocationRecordDao.selectByPrimaryKey(param.getArdId());
        return purAllocationRecordDao.loadAllocationRecord(pAllocationRecord);
    }

    /**
     * @FunctionName: findAllocationOrderListByPDBCodeAndArdType
     * @Description: 根据调拨单号和单据类型获取数据
     * @Author: 徐一贺
     * @CreateDate: 2020/4/7 16:13
     * @Phone: 18241927380
     * @Param: odrCode
     * @Return: List<PAllocationRecordModel>
     * @Version: 1.0.0
     */
    public List<PAllocationRecordModel> findAllocationOrderListByPDBCodeAndArdType(String odrCode) {
        return purAllocationRecordDao.findAllocationOrderListByPDBCodeAndArdType(odrCode);
    }

    /**
     * @FunctionName: checkIsChangeOrder
     * @Description: 检测是否是串料单物料
     * @Author: 徐一贺
     * @CreateDate: 2020/5/13 19:45
     * @Phone: 18241927380
     * @Param: [param.ityIds] 用来监测的库存主键
     * @Return: [boolean] 检测结果，是返回true，否返回false;
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean checkIsChangeOrder(PInventoryQueryModel param) {
        JsonResult jsonResult = new JsonResult();
        List<PInventoryModel> pInventoryModelList = purPInventoryDao.findInventoryListByIds(param.getItyIds());
        for (PInventoryModel pInventoryModel : pInventoryModelList) {
            PChangeOrderModel getListParamModel = new PChangeOrderModel();
            getListParamModel.setCeoUseItyId(pInventoryModel.getItyId());
            getListParamModel.setCeoAppyStatus(3);
            //首先用库存ID获取已执行的串料单被串料库存
            List<PChangeOrderModel> pChangeOrderModelList = purchangeDao.findPChangeOrderListSelective(getListParamModel);
            //如果没有，则获取已执行串料单，根据串料获取被串料
            if (pChangeOrderModelList.size() == 0) {
                getListParamModel.setCeoUseItyId(null);
                getListParamModel.setCeoNeedItyId(pInventoryModel.getItyId());
                pChangeOrderModelList = purchangeDao.findPChangeOrderListSelective(getListParamModel);
                if (pChangeOrderModelList.size() == 0) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * @FunctionName: getChangeAllocationData
     * @Description: 获取串料调拨数据
     * @DesignIdeas： 获取调拨所使用的相关数据，保存成调拨单，返回到前端页面
     * @Author: 徐一贺
     * @CreateDate: 2020/5/13 20:04
     * @Phone: 18241927380
     * @Param: [param] 参数
     * @Return: [result] 结果
     * @Version: 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public String getChangeAllocationData(PInventoryQueryModel param) {
        List<PInventoryModel> pInventoryModelList = purPInventoryDao.findInventoryModelListByIds(param.getItyIds());
        String dbCode = generatePDBOrderCode();
        for (PInventoryModel pInventoryModel : pInventoryModelList) {
            PChangeOrderModel getListParamModel = new PChangeOrderModel();
            getListParamModel.setCeoUseItyId(pInventoryModel.getItyId());
            getListParamModel.setCeoAppyStatus(3);
            //首先用库存ID获取已执行的串料单被串料库存
            List<PChangeOrderModel> pChangeOrderModelList = purchangeDao.findPChangeOrderListSelective(getListParamModel);
            if (pChangeOrderModelList.size() > 0) {
                Double totalNum = 0d;
                for (PChangeOrderModel pChangeOrderModel : pChangeOrderModelList) {
                    if (pChangeOrderModel.getCeoType() == 2) {
                        totalNum += pChangeOrderModel.getCeoNeedNum();
                    }
                }
                //调拨出库数据
                PAllocationRecordModel pAllocationRecordModel = new PAllocationRecordModel();
                pAllocationRecordModel.setArdItyId(getListParamModel.getCeoUseItyId());
                pAllocationRecordModel.setArdNum(totalNum + pInventoryModel.getItyNum());
                pAllocationRecordModel.setArdAmount(calculatorService.getAmount(pAllocationRecordModel.getArdNum(), pInventoryModel.getItyPrice()).doubleValue());
                pAllocationRecordModel.setArdSapMatDesc(pInventoryModel.getItyMatDesc());
                pAllocationRecordModel.setArdMatCode(pInventoryModel.getItySapCode());
                pAllocationRecordModel.setArdProName(pInventoryModel.getItyProName());
                pAllocationRecordModel.setArdProCode(pInventoryModel.getItyProCode());
                pAllocationRecordModel.setArdUnit(pInventoryModel.getItyNumUnit());
                pAllocationRecordModel.setArdWhiId(pInventoryModel.getItyWhiId());
                pAllocationRecordModel.setArdType(1);
                pAllocationRecordModel.setArdTakeType("制作中");
                pAllocationRecordModel.setArdExecStatus(0);
                pAllocationRecordModel.setArdChangeFlg(1);
                pAllocationRecordModel.setArdFormCode(dbCode);
                pAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                pAllocationRecordModel.setArdDepId(param.getDepId());
                pAllocationRecordModel.setArdDataStatus(1);
                pAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                pAllocationRecordModel.setArdGenDate(new Date());
                pAllocationRecordModel.setArdPosition(pInventoryModel.getItyPosition());
                pAllocationRecordModel.setArdTrackCode(pInventoryModel.getItyTrackCode());
                pAllocationRecordModel.setArdSpec(pInventoryModel.getItyRemark());
                pAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                pAllocationRecordModel.setArdRemark(param.getOdrRemark());
                pAllocationRecordModel.setArdDisable(0);
                pAllocationRecordModel.setArdBrand(pInventoryModel.getItyBrand());
                pAllocationRecordModel.setArdItyRemark(pInventoryModel.getItyRemark());
                pAllocationRecordModel.setArdPrice(pInventoryModel.getItyPrice());
                pAllocationRecordModel.setArdStockType(pInventoryModel.getItyStockType());
                purAllocationRecordDao.insertModelSelective(pAllocationRecordModel);
                Integer ardRelationId = pAllocationRecordModel.getArdId();
                Integer ceoId = 0;
                //调拨入库数据
                for (PChangeOrderModel pChangeOrderModel : pChangeOrderModelList) {
                    PInventoryModel inPInventoryModel = purPInventoryDao.getInventoryByPrimaryKey(pChangeOrderModel.getCeoNeedItyId());
                    PAllocationRecordModel inPAllocationRecordModel = new PAllocationRecordModel();
                    inPAllocationRecordModel.setArdPid(pAllocationRecordModel.getArdId());
                    inPAllocationRecordModel.setArdItyId(inPInventoryModel.getItyId());
                    inPAllocationRecordModel.setArdNum(inPInventoryModel.getItyNum());
                    inPAllocationRecordModel.setArdAmount(inPInventoryModel.getItyAmount());
                    inPAllocationRecordModel.setArdChangeDetailId(pChangeOrderModel.getCeoId());
                    ceoId = pChangeOrderModel.getCeoId();
                    inPAllocationRecordModel.setArdSapMatDesc(inPInventoryModel.getItyMatDesc());
                    inPAllocationRecordModel.setArdMatCode(inPInventoryModel.getItySapCode());
                    inPAllocationRecordModel.setArdProName(inPInventoryModel.getItyProName());
                    inPAllocationRecordModel.setArdProCode(inPInventoryModel.getItyProCode());
                    inPAllocationRecordModel.setArdUnit(inPInventoryModel.getItyNumUnit());
                    inPAllocationRecordModel.setArdWhiId(inPInventoryModel.getItyWhiId());
                    inPAllocationRecordModel.setArdType(2);
                    inPAllocationRecordModel.setArdExecStatus(0);
                    inPAllocationRecordModel.setArdChangeFlg(1);
                    inPAllocationRecordModel.setArdFormCode(dbCode);
                    inPAllocationRecordModel.setArdTakeType("制作中");
                    inPAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                    inPAllocationRecordModel.setArdDepId(param.getDepId());
                    inPAllocationRecordModel.setArdDataStatus(1);
                    inPAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                    inPAllocationRecordModel.setArdGenDate(new Date());
                    inPAllocationRecordModel.setArdPosition(inPInventoryModel.getItyPosition());
                    inPAllocationRecordModel.setArdTrackCode(inPInventoryModel.getItyTrackCode());
                    inPAllocationRecordModel.setArdSpec(inPInventoryModel.getItyRemark());
                    inPAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                    inPAllocationRecordModel.setArdRemark(param.getOdrRemark());
                    inPAllocationRecordModel.setArdDisable(0);
                    inPAllocationRecordModel.setArdBrand(inPInventoryModel.getItyBrand());
                    inPAllocationRecordModel.setArdItyRemark(inPInventoryModel.getItyRemark());
                    inPAllocationRecordModel.setArdPrice(inPInventoryModel.getItyPrice());
                    inPAllocationRecordModel.setArdStockType(inPInventoryModel.getItyStockType());
                    inPAllocationRecordModel.setArdRelationId(ardRelationId);
                    purAllocationRecordDao.insertModelSelective(inPAllocationRecordModel);
                }
                PAllocationRecordModel inPAllocationRecordModel = new PAllocationRecordModel();
                inPAllocationRecordModel.setArdPid(pAllocationRecordModel.getArdId());
                inPAllocationRecordModel.setArdItyId(pInventoryModel.getItyId());
                inPAllocationRecordModel.setArdNum(pInventoryModel.getItyNum());
                inPAllocationRecordModel.setArdAmount(pInventoryModel.getItyAmount());
                inPAllocationRecordModel.setArdChangeDetailId(ceoId);
                inPAllocationRecordModel.setArdSapMatDesc(pInventoryModel.getItyMatDesc());
                inPAllocationRecordModel.setArdMatCode(pInventoryModel.getItySapCode());
                inPAllocationRecordModel.setArdProName(pInventoryModel.getItyProName());
                inPAllocationRecordModel.setArdProCode(pInventoryModel.getItyProCode());
                inPAllocationRecordModel.setArdUnit(pInventoryModel.getItyNumUnit());
                inPAllocationRecordModel.setArdWhiId(pInventoryModel.getItyWhiId());
                inPAllocationRecordModel.setArdType(2);
                inPAllocationRecordModel.setArdTakeType("制作中");
                inPAllocationRecordModel.setArdExecStatus(0);
                inPAllocationRecordModel.setArdChangeFlg(1);
                inPAllocationRecordModel.setArdFormCode(dbCode);
                inPAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                inPAllocationRecordModel.setArdDepId(param.getDepId());
                inPAllocationRecordModel.setArdDataStatus(1);
                inPAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                inPAllocationRecordModel.setArdGenDate(new Date());
                inPAllocationRecordModel.setArdPosition(pInventoryModel.getItyPosition());
                inPAllocationRecordModel.setArdTrackCode(pInventoryModel.getItyTrackCode());
                inPAllocationRecordModel.setArdSpec(pInventoryModel.getItyRemark());
                inPAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                inPAllocationRecordModel.setArdRemark(param.getOdrRemark());
                inPAllocationRecordModel.setArdDisable(0);
                inPAllocationRecordModel.setArdBrand(pInventoryModel.getItyBrand());
                inPAllocationRecordModel.setArdItyRemark(pInventoryModel.getItyRemark());
                inPAllocationRecordModel.setArdPrice(pInventoryModel.getItyPrice());
                inPAllocationRecordModel.setArdStockType(pInventoryModel.getItyStockType());
                inPAllocationRecordModel.setArdRelationId(ardRelationId);
                purAllocationRecordDao.insertModelSelective(inPAllocationRecordModel);
                PAllocationRecord resultListParam = new PAllocationRecord();
                resultListParam.setArdFormCode(dbCode);
            }
            //如果没有，则获取已执行串料单，根据串料获取被串料
            else {
                getListParamModel.setCeoUseItyId(null);
                getListParamModel.setCeoAppyStatus(3);
                getListParamModel.setCeoNeedItyId(pInventoryModel.getItyId());
                pChangeOrderModelList = purchangeDao.findPChangeOrderListSelective(getListParamModel);
                if (pChangeOrderModelList.size() > 0) {
                    for (PChangeOrderModel pChangeOrderModel : pChangeOrderModelList) {
                        PInventoryModel pInventoryModelOther = purPInventoryDao.getInventoryByPrimaryKey(pChangeOrderModel.getCeoUseItyId());
                        getListParamModel.setCeoNeedItyId(null);
                        getListParamModel.setCeoUseItyId(pInventoryModelOther.getItyId());
                        pChangeOrderModelList = purchangeDao.findPChangeOrderListSelective(getListParamModel);
                        if (pChangeOrderModelList.size() > 0) {
                            Double totalNum = 0d;
                            for (PChangeOrderModel pChangeOrderModelOther : pChangeOrderModelList) {
                                if (pChangeOrderModel.getCeoType() == 2) {
                                    totalNum += pChangeOrderModelOther.getCeoNeedNum();
                                }
                            }
                            //调拨出库数据
                            PAllocationRecordModel pAllocationRecordModel = new PAllocationRecordModel();
                            pAllocationRecordModel.setArdItyId(getListParamModel.getCeoUseItyId());
                            pAllocationRecordModel.setArdNum(totalNum + pInventoryModel.getItyNum());
                            pAllocationRecordModel.setArdAmount(calculatorService.getAmount(pAllocationRecordModel.getArdNum(), pInventoryModel.getItyPrice()).doubleValue());
                            pAllocationRecordModel.setArdSapMatDesc(pInventoryModel.getItyMatDesc());
                            pAllocationRecordModel.setArdMatCode(pInventoryModel.getItySapCode());
                            pAllocationRecordModel.setArdProName(pInventoryModel.getItyProName());
                            pAllocationRecordModel.setArdProCode(pInventoryModel.getItyProCode());
                            pAllocationRecordModel.setArdUnit(pInventoryModel.getItyNumUnit());
                            pAllocationRecordModel.setArdWhiId(pInventoryModel.getItyWhiId());
                            pAllocationRecordModel.setArdType(1);
                            pAllocationRecordModel.setArdTakeType("制作中");
                            pAllocationRecordModel.setArdExecStatus(0);
                            pAllocationRecordModel.setArdChangeFlg(1);
                            pAllocationRecordModel.setArdFormCode(dbCode);
                            pAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                            pAllocationRecordModel.setArdDepId(param.getDepId());
                            pAllocationRecordModel.setArdDataStatus(1);
                            pAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                            pAllocationRecordModel.setArdGenDate(new Date());
                            pAllocationRecordModel.setArdPosition(pInventoryModel.getItyPosition());
                            pAllocationRecordModel.setArdTrackCode(pInventoryModel.getItyTrackCode());
                            pAllocationRecordModel.setArdSpec(pInventoryModel.getItyRemark());
                            pAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                            pAllocationRecordModel.setArdRemark(param.getOdrRemark());
                            pAllocationRecordModel.setArdDisable(0);
                            pAllocationRecordModel.setArdBrand(pInventoryModel.getItyBrand());
                            pAllocationRecordModel.setArdItyRemark(pInventoryModel.getItyRemark());
                            pAllocationRecordModel.setArdPrice(pInventoryModel.getItyPrice());
                            pAllocationRecordModel.setArdStockType(pInventoryModel.getItyStockType());
                            purAllocationRecordDao.insertModelSelective(pAllocationRecordModel);
                            Integer ardRelationId = pAllocationRecordModel.getArdId();
                            Integer ceoId = 0;
                            Integer ityId = 0;
                            //调拨入库数据
                            for (PChangeOrderModel pChangeOrderModelOther : pChangeOrderModelList) {
                                PInventoryModel inPInventoryModel = purPInventoryDao.getInventoryByPrimaryKey(pChangeOrderModelOther.getCeoNeedItyId());
                                PAllocationRecordModel inPAllocationRecordModel = new PAllocationRecordModel();
                                inPAllocationRecordModel.setArdItyId(inPInventoryModel.getItyId());
                                inPAllocationRecordModel.setArdPid(pAllocationRecordModel.getArdId());
                                inPAllocationRecordModel.setArdNum(inPInventoryModel.getItyNum());
                                inPAllocationRecordModel.setArdAmount(inPInventoryModel.getItyAmount());
                                inPAllocationRecordModel.setArdChangeDetailId(pChangeOrderModelOther.getCeoId());
                                ceoId = pChangeOrderModelOther.getCeoId();
                                inPAllocationRecordModel.setArdSapMatDesc(inPInventoryModel.getItyMatDesc());
                                inPAllocationRecordModel.setArdMatCode(inPInventoryModel.getItySapCode());
                                inPAllocationRecordModel.setArdProName(inPInventoryModel.getItyProName());
                                inPAllocationRecordModel.setArdProCode(inPInventoryModel.getItyProCode());
                                inPAllocationRecordModel.setArdUnit(inPInventoryModel.getItyNumUnit());
                                inPAllocationRecordModel.setArdWhiId(inPInventoryModel.getItyWhiId());
                                inPAllocationRecordModel.setArdType(2);
                                inPAllocationRecordModel.setArdTakeType("制作中");
                                inPAllocationRecordModel.setArdExecStatus(0);
                                inPAllocationRecordModel.setArdChangeFlg(1);
                                inPAllocationRecordModel.setArdFormCode(dbCode);
                                inPAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                                inPAllocationRecordModel.setArdDepId(param.getDepId());
                                inPAllocationRecordModel.setArdDataStatus(1);
                                inPAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                                inPAllocationRecordModel.setArdGenDate(new Date());
                                inPAllocationRecordModel.setArdPosition(inPInventoryModel.getItyPosition());
                                inPAllocationRecordModel.setArdTrackCode(inPInventoryModel.getItyTrackCode());
                                inPAllocationRecordModel.setArdSpec(inPInventoryModel.getItyRemark());
                                inPAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                                inPAllocationRecordModel.setArdRemark(param.getOdrRemark());
                                inPAllocationRecordModel.setArdDisable(0);
                                inPAllocationRecordModel.setArdBrand(inPInventoryModel.getItyBrand());
                                inPAllocationRecordModel.setArdItyRemark(inPInventoryModel.getItyRemark());
                                inPAllocationRecordModel.setArdPrice(inPInventoryModel.getItyPrice());
                                inPAllocationRecordModel.setArdStockType(inPInventoryModel.getItyStockType());
                                inPAllocationRecordModel.setArdRelationId(ardRelationId);
                                ityId = pChangeOrderModelOther.getCeoUseItyId();
                                purAllocationRecordDao.insertModelSelective(inPAllocationRecordModel);
                            }
                            pInventoryModel = purPInventoryDao.getInventoryByPrimaryKey(ityId);
                            if (pInventoryModel.getItyNum() > 0) {
                                PAllocationRecordModel inPAllocationRecordModel = new PAllocationRecordModel();
                                inPAllocationRecordModel.setArdItyId(pInventoryModel.getItyId());
                                inPAllocationRecordModel.setArdPid(pAllocationRecordModel.getArdId());
                                inPAllocationRecordModel.setArdNum(pInventoryModel.getItyNum());
                                inPAllocationRecordModel.setArdAmount(pInventoryModel.getItyAmount());
                                inPAllocationRecordModel.setArdChangeDetailId(ceoId);
                                inPAllocationRecordModel.setArdSapMatDesc(pInventoryModel.getItyMatDesc());
                                inPAllocationRecordModel.setArdMatCode(pInventoryModel.getItySapCode());
                                inPAllocationRecordModel.setArdProName(pInventoryModel.getItyProName());
                                inPAllocationRecordModel.setArdProCode(pInventoryModel.getItyProCode());
                                inPAllocationRecordModel.setArdUnit(pInventoryModel.getItyNumUnit());
                                inPAllocationRecordModel.setArdWhiId(pInventoryModel.getItyWhiId());
                                inPAllocationRecordModel.setArdType(2);
                                inPAllocationRecordModel.setArdExecStatus(0);
                                inPAllocationRecordModel.setArdChangeFlg(1);
                                inPAllocationRecordModel.setArdFormCode(dbCode);
                                inPAllocationRecordModel.setArdTakeType("制作中");
                                inPAllocationRecordModel.setArdRequester(param.getOdrTakePerson());
                                inPAllocationRecordModel.setArdDepId(param.getDepId());
                                inPAllocationRecordModel.setArdDataStatus(1);
                                inPAllocationRecordModel.setArdUserAccount(ShiroUtils.getUser().getAccount());
                                inPAllocationRecordModel.setArdGenDate(new Date());
                                inPAllocationRecordModel.setArdPosition(pInventoryModel.getItyPosition());
                                inPAllocationRecordModel.setArdTrackCode(pInventoryModel.getItyTrackCode());
                                inPAllocationRecordModel.setArdSpec(pInventoryModel.getItyRemark());
                                inPAllocationRecordModel.setArdInWhiId(param.getInWarehouseId());
                                inPAllocationRecordModel.setArdRemark(param.getOdrRemark());
                                inPAllocationRecordModel.setArdDisable(0);
                                inPAllocationRecordModel.setArdBrand(pInventoryModel.getItyBrand());
                                inPAllocationRecordModel.setArdItyRemark(pInventoryModel.getItyRemark());
                                inPAllocationRecordModel.setArdPrice(pInventoryModel.getItyPrice());
                                inPAllocationRecordModel.setArdStockType(pInventoryModel.getItyStockType());
                                inPAllocationRecordModel.setArdRelationId(ardRelationId);
                                purAllocationRecordDao.insertModelSelective(inPAllocationRecordModel);
                            }
                            PAllocationRecord resultListParam = new PAllocationRecord();
                            resultListParam.setArdFormCode(dbCode);


                        }
                    }
                }
            }

        }
        return dbCode;
    }

    /**
     * @FunctionName: save
     * @Description: 创建未执行的调拨单
     * @DesignIdea: 创建未执行的调拨单
     * @Author: 徐一贺
     * @CreateDate: 2020/5/19 16:25
     * @Phone: 18241927380
     * @Param:
     * @Return: 结果
     * @Throw:
     * @Version: v 1.0.0
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(PInventoryQueryModel param) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        List<String> warningList = new ArrayList<>();
        if (checkIsChangeOrder(param)) {
            jsonResult.setData(getChangeAllocationData(param));
            jsonResult.setMsg("串料调拨");
        } else {
            String odrCode;
            //如果单据号是null则创建单据
            if (param.getOdrCode() == null || param.getOdrCode().equals("")) {
                //创建调拨记录
                odrCode = createAllocationOrder(param);
            }
            //如果不为空则更新表单单据号的数据
            else {
                odrCode = param.getOdrCode();
                Set<PInventoryModel> existPInventoryModelSet = new HashSet<>();
                List<PAllocationRecord> storagePAllocationRecordList = purAllocationRecordDao.findPAllocationRecordListByArdFormCode(odrCode);
                List<PInventoryModel> pInventoryModelList = purPInventoryDao.findInventoryModelListByIds(param.getItyIds());

                for (PInventoryModel pInventoryModel : pInventoryModelList) {
                    for (PAllocationRecord pAllocationRecord : storagePAllocationRecordList) {
                        if (pAllocationRecord.getArdType().equals(1) && pInventoryModel.getItyId().equals(pAllocationRecord.getArdItyId())) {
                            existPInventoryModelSet.add(pInventoryModel);
                            warningList.add("调拨出库已存在物料描述为【" + pInventoryModel.getItyMatDesc() + "】的物料");
                        }
                    }
                }
                for (PInventoryModel pInventoryModel : existPInventoryModelSet) {
                    pInventoryModelList.remove(pInventoryModel);
                }
                createAllocationOrderHaveFormCode(pInventoryModelList, param);
            }
//            List<PAllocationRecordModel> pInventoryModelList = findAllocationOrderListByPDBCode(odrCode);
            logService.saveLog("创建调拨单", "调拨单号：" + odrCode);
            jsonResult.setData(odrCode);
            if (warningList.size() == 0) {
                jsonResult.setMsg("选择调拨库存成功！");
            } else {
                jsonResult.setMsg(warningList.toString().replace(",", "<br>"));
            }
        }
        return jsonResult;
    }

    /**
     * @FunctionName: projectCodeISExist
     * @Description: 验证项目号是否存在
     * @DesignIdea: 验证项目号是否存在
     * @Author: 徐一贺
     * @CreateDate: 2020/5/25 8:08
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: boolean
     * @Throw:
     * @Version: v 1.0.0
     */
    public boolean projectCodeISExist(PAllocationRecordModel param) {
        return getVWProjectByCode(param.getCode()) != null;
    }

    /**
     * @FunctionName: findAllocationList
     * @Description: 获取调拨数据
     * @DesignIdea: 根据调拨单号和调拨类型获取调拨数据
     * @Author: 徐一贺
     * @CreateDate: 2020/5/25 14:58
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Throw: 无异常抛出
     * @Version: v 1.0.0
     */
    public List<PAllocationRecordModel> findAllocationList(PAllocationRecordModel param) {
        return purAllocationRecordDao.findAllocationList(param);
    }

    /**
     * @FunctionName: findAllocationSelfContainAndChildrenList
     * @Description: 根据ardPid获取子级以及自己的数据列表
     * @Author: 徐一贺
     * @CreateDate: 2020/5/25 16:41
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: com.zhongwang.mms.common.JsonResult
     * @Version: v 1.0.0
     */
    public List<PAllocationRecordModel> findAllocationSelfContainAndChildrenList(PAllocationRecordModel param) {
        return purAllocationRecordDao.findAllocationSelfContainAndChildrenList(param);
    }

    /**
     * @FunctionName: codeExist
     * @Description: 验证项目号是否存在
     * @DesignIdea: 验证项目号是否存在
     * @Author: 徐一贺
     * @CreateDate: 2020/5/26 10:02
     * @Phone: 18241927380
     * @Param: [param]
     * @Return: java.lang.String
     * @Version: v 2.0.0
     */
    public Boolean codeExist(VWProject param) {
        VWProject vwProject = purAllocationRecordDao.findVWProjectByCode(param);
        return vwProject != null;
    }
}